import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { BusiserviceProvider } from '../../providers/busiservice/busiservice';
import { UiserviceProvider } from '../../providers/uiservice/uiservice';

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {
  username: string;
  password: string;
  type: number;
  constructor(public navCtrl: NavController, public navParams: NavParams, public busi: BusiserviceProvider, private uiservice: UiserviceProvider) {
    this.type = this.navParams.get("type");
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }

  login() {
    if (this.username == null) {
      alert("User Name is Empty");
      return;
    }
    if (this.password == null) {
      alert("Password is Empty！");
      return;
    }
    this.uiservice.showLoading();
    this.busi.login(this.username, this.password, this.type).subscribe(
      data => {
        this.uiservice.hideLoading();
        if (data._success) {
          //用户id
          localStorage.setItem("userid", data.result.userid);
          //求职者
          if (this.type == 1) {
            this.navCtrl.push("QzzPage");
          }
          //HR
          if (this.type == 2) {
            this.navCtrl.push("ZwfbPage");
          }
        } else {
          this.uiservice.showAlert(data._message);
        }
      },
      error => {
        this.uiservice.hideLoading();
        this.uiservice.showAlert("Net Error");
      }
    );
  }
}
